<?php

namespace App\Service;

use App\Constants\ErrorCode;
use App\Model\DistributionCenterTag;
use App\Model\LinkIndustry;
use App\Model\Specialty;
use Hyperf\DbConnection\Db;
use Hyperf\Snowflake\IdGeneratorInterface;
use Hyperf\Utils\ApplicationContext;
use Psr\Container\ContainerInterface;
use Hyperf\Di\Annotation\Inject;

class SpecialtyService extends BaseService
{



    public function get_list($params,$fields=['*'])
    {
        // 关键字：行业名称
        $key_word = $params['key_word']?$params['key_word']:'';
        $is_show = $params['is_show']?$params['is_show']:'';
        $ids = $params['ids']?$params['ids']:[];
        $Model = New Specialty();
        $where = [];
        if($is_show!=''){
            $where['is_show'] = $is_show;
        }
        $specialty_ids = [];
        if($ids){
            $specialty_ids = $this->linkfind($ids);
        }
        $count = $Model
            ->where($where)
            ->where(function ($query) use ($key_word) {
                if (!empty($key_word)) {
                    $query->where('industry_name', 'like', '%' . $key_word . '%');
                }
            })
            ->where(function ($query) use ($ids,$specialty_ids) {
                if ($ids!==[]) {
                    $query->whereIn('id',$specialty_ids);
                }
            })
            ->count();
        if($count>0){
            $industry_model = $Model
                ->where($where)
                ->where(function ($query) use ($key_word) {
                    if (!empty($key_word)) {
                        $query->where('industry_name', 'like', '%' . $key_word . '%');
                    }
                })
                ->where(function ($query) use ($ids,$specialty_ids) {
                    if ($ids!==[]) {
                        $query->whereIn('id',$specialty_ids);
                    }
                })
                ->with('create_info')
                ->with('update_info')
                ->orderBy('id','desc')
                ->select($fields);
            $list = $this->paginate($industry_model);
        }
        return $list;
    }

    //   新增单条  包括关联关系
    public function insert(array  $insertData)
    {
        $industry = $insertData['industry'];unset($insertData['industry']);
        $id = Specialty::query()->insertGetId($insertData);
        $industry_data  = [];
        foreach ($industry as $v){
            $industry_data[] = [
                'industry_id'=>$v,
                'link_id'=>$id,
                'type'=>3,
            ];
        }
        if(count($industry_data)>0){
            LinkIndustry::query()->insert($industry_data);
        }
        return  true;
    }
    public function insertGetId(array  $insertData)
    {
        return Specialty::query()->insertGetId($insertData);
    }

//    更新包括关联关系
    public function update(array  $upData)
    {
//        先清空旧数据
        LinkIndustry::query()->where(['type'=>2,'link_id'=>$upData['id']])->delete();

        $industry = $upData['industry'];unset($upData['industry']);
        $industry_data  = [];
        foreach ($industry as $v){
            $industry_data[] = [
                'industry_id'=>$v,
                'link_id'=>$upData['id'],
                'type'=>3,
            ];
        }
        if(count($industry_data)>0){
            LinkIndustry::query()->insert($industry_data);
        }
        return Specialty::query()->where('id',$upData['id'])->update($upData);
    }

//    查找全部行业下的企业ids
    public function linkfind(array $ids)
    {
        $link_id = LinkIndustry::query()->where('type',3)->whereIn('industry_id',$ids)->pluck('link_id');
        if($link_id){
            return $link_id->toArray();
        }else{
            return [];
        }
    }
}